if (!global.ShadowMamba) {
global.ShadowMamba = {};
 }
var sm = global.ShadowMamba;

function sm_createItemOnFail(f) {
//Really only useful when you're throwing a sword or dagger at an opponent and want to retrieve it if it missed.
var source = f.source_unit;
var target = f.target_unit;
var tile = f.target_tile;
var item = f.item;
var flags = item.ScriptFlags;
if (flags.get("create_items")) {
var item = flags.get("create_items");
tile.Inventory.CreateAndAddItem(item);
}
}

function sm_createItems(c) {
var source = c.source_unit;
var target = c.target_unit;
var tile = c.target_tile;
var skill = c.skill;
var item = c.item;
var flags;
if (skill)
flags = skill.ScriptFlags;
else if (item)
flags = item.ScriptFlags;
if (flags.get("create_items")) {
var math = Math.random().toFixed(2);
/*if (flags.get("create_items_chance")) {
var chance = flags.get("create_items_chance");
if (math > chance) {
}
}*/
if (flags.get("create_rare_items")) {
if (flags.get("create_rare_items_chance")) {
var rareItems = chooseFromARandomSelectString(flags.get("create_rare_items"));
var createRareItems = rareItems.split(",");
var chance = flags.get("create_rare_items_chance");
if (math > chance) {
var normalItems = chooseFromARandomSelectString(flags.get("create_items"));
var createItems = normalItems.split(",");
for (var i = 0; i < createItems.length; i++) {
if (target)
target.Inventory.CreateAndAddItem(createItems[i]);
else if (!target)
tile.Inventory.CreateAndAddItem(createItems[i]);
}
if (flags.get("creation_message")) {
var msg = chooseFromARandomSelectString(flags.get("creation_message"));
var items = createItems;
for (var i = 0; i < items.length; i++) {
if (target)
say((msg).format(source.GetName(),items[i],target.GetName()));
else if (!target)
say((msg).format(source.GetName(),items[i]));
}
}
}
else if (math <= chance) {
for (var i = 0; i < createRareItems.length; i++) {
if (target)
target.Inventory.CreateAndAddItem(createRareItems[i]);
else if (!target)
tile.Inventory.CreateAndAddItem(createRareItems[i]);
}
if (flags.get("creation_message")) {
var msg = chooseFromARandomSelectString(flags.get("creation_message"));
var items = createRareItems;
for (var i = 0; i < items.length; i++) {
if (target)
say((msg).format(source.GetName(),items[i],target.GetName()));
else if (!target)
say((msg).format(source.GetName(),items[i]));
}
}
}
}
if (!flags.get("create_rare_items_chance")) {
say("Error: You must use the create rare items chance flag alongside the create rare items flag!");
}
}
if (!flags.get("create_rare_items")) {
var normalItems = chooseFromARandomSelectString(flags.get("create_items"));
var createItems = normalItems.split(",");
for (var i = 0; i < createItems.length; i++) {
if (target)
target.Inventory.CreateAndAddItem(createItems[i]);
else if (!target)
tile.Inventory.CreateAndAddItem(createItems[i]);
}
if (flags.get("creation_message")) {
var msg = chooseFromARandomSelectString(flags.get("creation_message"));
var items = createItems;
for (var i = 0; i < items.length; i++) {
if (target)
say((msg).format(source.GetName(),items[i],target.GetName()));
else if (!target)
say((msg).format(source.GetName(),items[i]));
}
}
}
}
}

function sm_stealItem(s) {
var source = s.source_unit;
var target = s.target_unit;
var skill = s.skill;
var flags = skill.ScriptFlags;
if (flags.get("steal_item")) {
var targetItems = target.Inventory.Items;
if (targetItems.length >0) {
var inventory = source.Inventory;
//choose a random item to steal
var itemToSteal = targetItems[Math.round(Math.random() * (targetItems.length - 1))];
//check if itemToSteal is a stacked item, and if so, only steal one of the item.
if (itemToSteal.Quantity > 1) {
itemToSteal.Quantity--;
var itemName = itemToSteal.IndexedName;
// try to find the item in the source's inventory
var existingItem = findItemWithName(inventory, itemName);
if (existingItem)
existingItem.Quantity++;
else
inventory.CreateAndAddItem(itemName);
}
else {
source.Inventory.Add(itemToSteal);
}
if (flags.get("steal_item_message")) {
var msg = chooseFromARandomSelectString(flags.get("steal_item_message"));
say((msg).format(source.GetName(),target.GetName(),itemToSteal.FriendlyName));
}
}
if (targetItems.length <1) {
say("This unit does not have any items to steal.")
}
}
}

function findItemWithName(inventory, itemName) {
if (inventory.Items.length < 1)
return null; // this is an empty inventory, so it doesn't contain the item
for (var index = 0; index < inventory.Items.length; index++) {
var currentItem = inventory.Items[index];
if (currentItem.IndexedName == itemName)
return currentItem; // there it is
}
return null; // we've gone through all the items, no match
}

function sm_randomInventoryItems(c, str) {
var unit = c.unit;
var flags = unit.ScriptFlags;
if (flags.get("random_inventory_items")) {
var str = flags.get("random_inventory_items");
str = str.split(";"); //str is now an 1 dimmensional array. Elements are strings extracted from between the commas of the scriptflag.
for (var count = 0; count < str.length; count++) {
//split each element of this array into a local array of item names and chances.
var split2 = str[count].split("/")
//calculate whether the item should be created.
var hasPercent = false;
if (split2[1]) hasPercent = true;
var work = true //make the item always load if there is no chance specified.
if (hasPercent) {
chance = parseInt(split2[1]);
if (chance<=0||chance>100) chance = 100;
if (shared.Calculate("1r100")>chance) work = false;
}
if (work) {
var randomItemChoice = chooseFromARandomSelectString(split2[0]);
randomItemChoice = randomItemChoice.split(",");
for (var index = 0; index < randomItemChoice.length; index++) {
unit.Inventory.CreateAndAddItem(randomItemChoice[index]);
}
}
}
}
}

/*
function removeItems(r) {
var source = r.source_unit;
var target = r.target_unit;
var tile = r.target_tile;
var skill = r.skill;
var item = r.item;
var flags;
if (skill)
flags = skill.ScriptFlags;
else if (item)
flags = item.ScriptFlags;
if (flags.get("remove_items")) {
var inventory = tile.Inventory;



}
*/
